package prac;

import java.util.*;

/**
 *
 6
 4815 1A1B
 5716 0A1B
 7842 0A1B
 4901 0A0B
  8585 3A0B
  8555 2A1B

 result 35850
 */
public class CaiShuzi {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 读取并处理第一个输入
        int n = scanner.nextInt();
        Map<Integer,String> rescord=new HashMap<>();
        List<Integer> shuru=new ArrayList<>();
        for(int i=0;i<n;i++){
            int sz=scanner.nextInt();
            String sz2=scanner.next();
            rescord.put(sz,sz2);
            shuru.add(sz);
        }
        String result="";
        for(Integer i=1000;i<10000;i++){
            Boolean isMatch=true;
            for(Integer j:shuru){
             String rest=cul(i,j);
             if(!rest.equals(rescord.get(j))){
                 isMatch=false;
                 break;
             }
            }
            if(isMatch){
            if(result.isEmpty()){
                result=i.toString();
            }else{
                System.out.println("NA");
                return;
            }
            }
        }
        if(result.isEmpty()){
            System.out.println("NA");
        }else {
            System.out.println(result);
        }
    }

    private static  String cul(Integer sr1,Integer sr2){
        Integer matchA=0;
        Integer matchB=0;
        String[] is= sr1.toString().split("");
        List<String> isList=Arrays.asList(is);
        String[] js1= sr2.toString().split("");
        List<String> js1List=Arrays.asList(js1);
        Set<Integer> tr=new HashSet<>();
        for(int i=0;i<4;i++){
            if(isList.get(i).equals(js1List.get(i))){
                tr.add(i);
                matchA+=1;
            }
        }

        for(int i:tr){
            isList.set(i,"");
            js1List.set(i,"");;
        }
        for(String i :js1List){
            if(!i.isEmpty()&&isList.contains(i)){
                matchB+=1;
            }
        }

        return matchA+"A"+matchB+"B";
    }
}
